<?php

/**
 * @filename File.class.php 
 * @encoding UTF-8 
 * @author  fwz 
 * @datetime 2018-6-22  9:27:01
 */



class File{
    
    private static $instance;  //实例
    protected $online_dir;     //在线目录地址
    protected $history = array();
    protected $history_max_size = 100;
    protected $history_write_count = 0;
    
    private function __construct() {
        global $rooms;
        $this->online_dir = ONLINE_DIR;
        foreach($rooms as $key=>$val){
            $this->checkDir($this->online_dir.$key.'/',true);
        }
    }
    
    //
    public static function init(){
        if(self::$instance instanceof  self){
            return false;
        }
        self::$instance = new self();
    }
    
    //
    public static function clearDir($dir){
        $n =0;
        if($dh = opendir($dir)){
            while(($file = readdir($dh)) !== false){
                if($file == '.' or $file == '..'){
                    continue;
                }
                if(is_file($dir.$file)){
                    unlink($dir.$file);
                    $n++;
                }
                if(is_dir($dir.$file)){
                    self::clearDir($dir.$file);
                    $n++;
                }
            }
        }
        closedir($dh);
        return $n;
    }
    
    //
    public static  function checkDir($dir,$clear_file=false){
        if(!is_dir($dir)){
            if(mkdir($dir,0777,TRUE)){
                rw_deny:
                trigger_error("can not read/write dir[".$dir."]",E_ERROR);
                return ;
            }
        }else if($clear_file){
            self::clearDir($dir);
        }
    }
    
    public static  function getOnlineUsers(){
        global $rooms;
        $online_users = array();
        foreach ($rooms as $key => $value) {
             
            $online_users[$key] = array_slice(scandir(self::$instance->online_dir.$key."/"), 2);
        }
        return $online_users;
    }
    
    public static function getUsers($roomid,$users){
        $ret = array();
        foreach($users as $key=>$val){
            $ret[] = self::getUser($roomid, $val);
        }
        return $ret;
    }
    
    
    public static function getUser($roomid,$userid){
        if($roomid == ''){
            global $rooms;
            foreach ($rooms as $key => $value) {
                if(file_exists(self::$instance->online_dir.$key."/".$userid)){
                    $roomid = $value;
                    break;
                }
            }
        }
        $ret = @file_get_contents(self::$instance->online_dir.$roomid."/".$userid);
        $info = unserialize($ret);
        $info['roomid'] = $roomid;
        return $info;
    }
    
    public static function login($roomid,$fd,$info){
        
        $flag = @file_put_contents(self::$instance->online_dir.$roomid."/".$fd, @serialize($info));
        return $flag;
    }
    
    
    public static function getUsersByRoom($roomid){
        $users = array_slice(scandir(self::$instance->online_dir.roomid.'/'), 2);
        return $users;
    }
    
    public static function Logout($userid){
        global $rooms;
        foreach ($rooms as $key=>$val){
            if(is_file(self::$instance->online_dir.$key."/".$userid)){
                unlink(self::$instance->online_dir.$key."/".$userid);
                break;
            }
        }
    }
    
    public static function changeUser($oldroomid,$fd,$newroomid){
        $old = self::$instance->online_dir.$oldroomid."/".$fd;
        $new = self::$instance->online_dir.$newroomid."/".$fd;
        
        $retrun = copy($old, $new);
        unlink($old);
        return $retrun;
    }
    
}

